package com.controller;

import cn.hutool.db.Db;
import com.alibaba.fastjson.JSONObject;
import com.bean.ZjMusic;
import com.bean.other.ResponseData;
import com.config.Commons;
import com.service.impl.ApiInfoService;
import com.service.impl.ZjMusicService;
import com.utils.DbUtil;
import com.utils.ResUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.time.Duration;

@RestController
@RequestMapping("test")
@CrossOrigin
@Slf4j
public class TestController {

    @GetMapping("testFlux")
    public Flux<String> testFlux(){
        return Flux.interval(Duration.ofSeconds(1))  // 每隔1秒触发
                .map(sequence -> "Real-time data #" + sequence);  // 输出数据
    }

    @Autowired
    private ApiInfoService apiInfoService;

    //每日搞笑段子
    @GetMapping("gxdz")
    public ResponseEntity<JSONObject> gxdz(){
        return ResponseEntity.ok(apiInfoService.ApiGetGxdz());
    }


    @Autowired
    private DbUtil dbUtil;

    @GetMapping("backDb")
    public ResponseEntity backDb(){
        try {
            dbUtil.DbBack();
            return ResponseEntity.ok(ResponseData.success("备份成功"));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return ResponseEntity.ok(ResponseData.error(e.getMessage()));
        }
    }

    //网易云随机音乐
    @GetMapping("sjmusic")
    public ResponseEntity<JSONObject> sjmusic(@RequestParam("type") String type){
        log.info("type"+type);
        return ResponseEntity.ok(apiInfoService.sjmusic(type));
    }

    @Autowired
    private ZjMusicService zjMusicService;

    @Autowired
    private ResUtil resUtil;

    @PostMapping("add")
    public ResponseEntity addMusicFromApi(@RequestBody ZjMusic zjMusic){
        String message = zjMusicService.addMusicFromApi(zjMusic);
        return ResponseEntity.ok(resUtil.returnByMessage(message));
    }


}
